package com.ghost.librariandb;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JFormattedTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class ReturnBook extends JPanel
{
	private JButton btnGetBook;
	private JFormattedTextField txtBookID;
	private JLabel lblBookID;
	private JLabel lblBookName;
	private JLabel lblAuthor;
	private JLabel lblBorrowerID;
	private JLabel lblBorrowerName;
	private JButton btnReturnBook;
	private int borrowerID = 0;

	/**
	 * Create the panel.
	 */
	public ReturnBook()
	{
		setLayout(null);
		
		JLabel label = new JLabel("Book ID:");
		label.setBounds(10, 15, 46, 14);
		add(label);
		
		txtBookID = new JFormattedTextField(new Integer(0));
		txtBookID.setBounds(70, 12, 127, 20);
		add(txtBookID);
		
		btnGetBook = new JButton("Get Book");
		btnGetBook.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				int bookID = (int) txtBookID.getValue();
				if (bookID > 0)
				{
					Connection connection = DataProvider.getConnection();
					Statement statement = null;
					try
					{
						statement = connection.createStatement();
						statement.setQueryTimeout(30);
						ResultSet rs = statement.executeQuery("select * from Book where ID = " + Integer.toString(bookID));
						int count = 0;
						while (rs.next())
						{
							count++;
							lblBookID.setText(rs.getString("ID"));
							lblBookName.setText(rs.getString("Name"));
							lblAuthor.setText(rs.getString("Author"));
							lblBorrowerID.setText(Integer.toString(rs.getInt("BorrowerID")));
							borrowerID = rs.getInt("BorrowerID");
							if (borrowerID > 0)
							{
								btnReturnBook.setEnabled(true);
							}
							else
							{
								lblBorrowerID.setText("");
								lblBorrowerName.setText("");
								btnReturnBook.setEnabled(false);
							}
							break;
						}
						if (borrowerID > 0)
						{
							rs = statement.executeQuery("select * from Member where ID = " + Integer.toString(borrowerID));
							while (rs.next())
							{
								lblBorrowerName.setText(rs.getString("Name"));
								break;
							}
						}
						
						if (count == 0)
						{
							lblBookID.setText("");
							lblBookName.setText("");
							lblAuthor.setText("");
							lblBorrowerID.setText("");
							lblBorrowerName.setText("");
						}
						System.out.println("[Book] Record found: " + count);
					}
					catch (SQLException ex)
					{
						ex.printStackTrace();
					}
					finally
					{
						if (statement != null)
						{
							try
							{
								statement.close();
							}
							catch (SQLException ex)
							{
								ex.printStackTrace();
							}
						}
					}
				}
			}
		});
		btnGetBook.setBounds(200, 11, 89, 23);
		add(btnGetBook);
		
		JLabel label_1 = new JLabel("Book ID:");
		label_1.setBounds(10, 54, 90, 14);
		add(label_1);
		
		JLabel label_2 = new JLabel("Book Name:");
		label_2.setBounds(10, 70, 90, 14);
		add(label_2);
		
		JLabel label_3 = new JLabel("Author:");
		label_3.setBounds(10, 85, 90, 14);
		add(label_3);
		
		JLabel label_4 = new JLabel("Borrower ID:");
		label_4.setBounds(10, 110, 90, 14);
		add(label_4);
		
		JLabel label_5 = new JLabel("Borrower Name:");
		label_5.setBounds(10, 135, 102, 14);
		add(label_5);
		
		lblBorrowerName = new JLabel("");
		lblBorrowerName.setBounds(122, 136, 200, 14);
		add(lblBorrowerName);
		
		lblAuthor = new JLabel("");
		lblAuthor.setBounds(122, 85, 256, 14);
		add(lblAuthor);
		
		lblBookName = new JLabel("");
		lblBookName.setBounds(122, 70, 256, 14);
		add(lblBookName);
		
		lblBookID = new JLabel("");
		lblBookID.setBounds(122, 54, 256, 14);
		add(lblBookID);
		
		btnReturnBook = new JButton("Return Book");
		btnReturnBook.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				Date now = new Date();
				Connection connection = DataProvider.getConnection();
				Statement statement = null;
				try
				{
					statement = connection.createStatement();
					statement.setQueryTimeout(30);
					String sql = "update Book set BorrowerID = 0, ReturnedDate = " + Long.toString(now.getTime()) + " where ID = " + lblBookID.getText();
					System.out.println(sql);
					statement.executeUpdate(sql);
					lblBorrowerID.setText("");
					lblBorrowerName.setText("");
					btnReturnBook.setEnabled(false);
				}
				catch (SQLException ex)
				{
					ex.printStackTrace();
				}
				finally
				{
					if (statement != null)
					{
						try
						{
							statement.close();
						}
						catch (SQLException ex)
						{
							ex.printStackTrace();
						}
					}
				}
			}
		});
		btnReturnBook.setEnabled(false);
		btnReturnBook.setBounds(122, 173, 127, 23);
		add(btnReturnBook);
		
		lblBorrowerID = new JLabel("");
		lblBorrowerID.setBounds(122, 110, 127, 14);
		add(lblBorrowerID);

	}
}
